package com.product.result;

import com.product.enmu.HttpStatusEnum;
import lombok.Data;

import java.io.Serializable;

@Data
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = -4350499690382193884L;

    /**
     * 返回状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public static <T> ApiResult<T> ok(T data, String message) {
        ApiResult<T> webResult = ok(data);
        webResult.setMessage("操作成功");
        return webResult;
    }

    public static <T> ApiResult<T> ok() {
        ApiResult<T> webResult = new ApiResult<>();
        webResult.setCode(HttpStatusEnum.OK.getCode());
        webResult.setMessage("操作成功");
        return webResult;
    }

    public static <T> ApiResult<T> ok(T data) {
        ApiResult<T> webResult = new ApiResult<>();
        webResult.setCode(HttpStatusEnum.OK.getCode());
        webResult.setMessage("操作成功");
        webResult.setData(data);
        return webResult;
    }

    public static <T> ApiResult<T> error() {
        ApiResult<T> webResult = new ApiResult<>();
        webResult.setCode(HttpStatusEnum.ERROR.getCode());
        webResult.setMessage("操作失败");
        return webResult;
    }

    public static <T> ApiResult<T> getResult(T data, int code, String message) {
        ApiResult<T> webResult = new ApiResult<>();
        webResult.setData(data);
        webResult.setCode(code);
        webResult.setMessage(message);
        return webResult;
    }

    public ApiResult message(String message) {
        this.setMessage(message);
        return this;
    }

    public ApiResult data(T data) {
        this.setData(data);
        return this;
    }

}
